package com.decent.manager.advice.handler;


import com.decent.common.bo.MessageBean;
import com.decent.common.config.advice.BaseExceptionHandler;
import com.decent.common.enums.ErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.message.AuthException;
import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author decent
 * @date 2020-12-22
 */
@Slf4j
@ControllerAdvice
public class ManagerExceptionHandler extends BaseExceptionHandler {
    /**
     * 自定义异常
     *
     * @param request 请求
     * @param e       异常
     * @return 返回值
     */
    @ExceptionHandler(value = AuthException.class)
    @ResponseBody
    public MessageBean errorAuthHandler(HttpServletRequest request, AuthException e) {
        MessageBean message = new MessageBean();
        log.warn("[{}]账号[{}]没有配置权限[{}]", request.getRequestURI(), "1123", e.getMessage(), e);
        message.setCode(ErrorCodeEnum.NO.getCode());
        message.setMessage("当前账号没有配置权限");
        return message;
    }

    /**
     * 兜底任意异常
     *
     * @param request 请求
     * @param ex      异常
     * @return MessageBean
     */
    @Override
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public MessageBean errorHandler(HttpServletRequest request, Exception ex) {
        MessageBean message = new MessageBean();
        log.error("[{}]系统异常", request.getRequestURI(), ex);
        message.setCode(ErrorCodeEnum.ERROR.getCode());
        message.setMessage(ErrorCodeEnum.ERROR.getMessage());
        return message;
    }

}